package main

import (
	"fmt"
	"net/http"
	"net/url"
	"strings"
	"io/ioutil"
)

func main() {
	var userId, password string

	fmt.Println("请输入学号:")
	fmt.Scanln(&userId)
	fmt.Println("请输入密码:")
	fmt.Scanln(&password)

	urlGetService := "http://172.30.0.11:80/eportal/userV2.do?method=getServices"
	dataGetService := url.Values{}
	dataGetService.Set("username", userId)
	dataGetService.Set("search", "")

	resp, err := http.PostForm(urlGetService, dataGetService)
	if err != nil {
		fmt.Println("Error:", err)
		return
	}
	defer resp.Body.Close()

	body, err := ioutil.ReadAll(resp.Body)
	if err != nil {
		fmt.Println("Error:", err)
		return
	}

	service := url.QueryEscape(strings.Replace(strings.Replace(string(body), "校园网", "", -1), "@", "", -1))

	urlWlanUserip := "http://172.30.0.11"
	resp, err = http.Get(urlWlanUserip)
	if err != nil {
		fmt.Println("Error:", err)
		return
	}
	defer resp.Body.Close()

	body, err = ioutil.ReadAll(resp.Body)
	if err != nil {
		fmt.Println("Error:", err)
		return
	}

	wlanUserip := strings.Replace(strings.Replace(string(body), "<script>top.self.location.href='http://172.30.0.11/eportal/index.jsp?", "", -1), "'</script>", "", -1)

	urlStartConnect := "http://172.30.0.11:80/eportal/InterFace.do?method=login"
	dataStartConnect := url.Values{}
	dataStartConnect.Set("userId", userId)
	dataStartConnect.Set("password", password)
	dataStartConnect.Set("service", service)
	dataStartConnect.Set("queryString", wlanUserip)
	dataStartConnect.Set("operatorPwd", "")
	dataStartConnect.Set("operatorUserId", "")
	dataStartConnect.Set("validcode", "")
	dataStartConnect.Set("passwordEncrypt", "false")

	resp, err = http.PostForm(urlStartConnect, dataStartConnect)
	if err != nil {
		fmt.Println("Error:", err)
		return
	}
	defer resp.Body.Close()

	body, err = ioutil.ReadAll(resp.Body)
	if err != nil {
		fmt.Println("Error:", err)
		return
	}

	fmt.Println(string(body))
}
